import __AS3__.vec.Vector;

import com.skt.common_hr.common.AppModuleManager;
import com.skt.common_hr.common.PsHrAlert;
import com.skt.common_hr.common.Variables;
import com.skt.common_hr.common.puremvc.CommonProxy;
import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
import com.skt.common_hr.yearadjustment.model.BasicViewInfoProxy;
import com.skt.common_hr.yearadjustment.view.YearAdjustmentMediator;
import com.skt.common_hr.yearadjustment.view.components.YearAdjustmentView;
import com.skt.common_hr.yearadjustment.view.components.popHouseNotice;
import com.wcl.utils.DateUtil;

import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.controls.ComboBox;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.utils.ObjectUtil;

import org.puremvc.as3.multicore.patterns.facade.Facade;
			
private static const NAME = "popHouseNoticeProxy";
public static var data1:ArrayCollection;
public static var data2:ArrayCollection;
public static var data2_0:ArrayCollection;
public static var data2_1:ArrayCollection;
public static var data2_2:ArrayCollection;
public static var data2_3:ArrayCollection;
public static var data2_4:ArrayCollection;

private var headerCount:uint;
private var data2Header:Vector.<String> = new Vector.<String>();

private var commProxy:CommonProxy;
private var yearAdjustmentMediator:YearAdjustmentMediator;

public var msgStr:String;
			
private var sealYear:String;
private var yearPernr:String;
private function init():void{
	this.commProxy = new CommonProxy(NAME);
	
	var basicViewInfoProxy:BasicViewInfoProxy = Facade.getInstance(AppModuleManager.MODULE_KEY_YEARADJUSTMENT).retrieveProxy(BasicViewInfoProxy.NAME) as BasicViewInfoProxy;
	
	yearAdjustmentMediator = Facade.getInstance(AppModuleManager.MODULE_KEY_YEARADJUSTMENT).retrieveMediator(YearAdjustmentMediator.NAME) as YearAdjustmentMediator;
	var yearAdjustmentView:YearAdjustmentView = YearAdjustmentView(yearAdjustmentMediator.getViewComponent());
	sealYear=yearAdjustmentView.selYear;
	yearPernr=Variables.yearPernr;
	
	data2Header[0] = new String("ZDHRSUR01");
	data2Header[1] = new String("ZDHRSUR02");
	data2Header[2] = new String("ZDHRSUR031");
	data2Header[3] = new String("ZDHRSUR033");
	data2Header[4] = new String("ZDHRSUR04");

	var acHshld:ArrayCollection = new ArrayCollection();
	acHshld.addItem({"TEXT":"No", "CODE":"N"});
	acHshld.addItem({"TEXT":"Yes", "CODE":"Y"});
	cbHSHLD.dataProvider = acHshld;
				
	//if(data1==null)loadData1(yearPernr,sealYear);
	loadData1(yearPernr,sealYear);
}
			
private function closePop(event:Event):void{
	PopUpManager.removePopUp(this);
}	


private function loadData1(pernr:String, year:String):void{
	var param:Object = new Object();
	param["I_YEAR"] = year;
	param["I_PERNR"] = pernr;
	param["I_PROC_TYPE"] = "D";		//D:조회, S:저장

	this.commProxy.exLoadData("Z_HR_HOUSE_SURVEY", param, result1, fault);
}

private function loadData2(header:uint):void{
	this.commProxy.exLoadData("Z_HR_0003", {I_DNAME:data2Header[header]}, result2, fault);
}

public function result1(event:ResultEvent):void
{
	if(event.result["RETCODE"]==0) {
		data1 = event.result["T_ITAB"];
		this.setMainTitle(data1);
		loadData2(this.headerCount=0);
	}
	else {
		msgStr = event.result["RETTEXT"];
		PsHrAlert.showMessage(msgStr);
	}
}

public function result2(event:ResultEvent):void{
	if(event.result["RETCODE"]==0) {
		var data2:ArrayCollection = event.result["ITAB"];
		this.setComboBox(data2);
		if(this.headerCount<data2Header.length-1){
			this.loadData2(++this.headerCount);
		}else{
			this.setSubTitle();
		}
	}else {
		msgStr = event.result["RETTEXT"];
		PsHrAlert.showMessage(msgStr);
	}
}

private function fault(event:FaultEvent):void
{
	msgStr = event.fault.faultString;
	PsHrAlert.showMessage(msgStr);
}

public function save(pernr:String, year:String, arrayList:Array):void{
	var param:Object = new Object();
	param["I_YEAR"] = year;
	param["I_PERNR"] = pernr;
	param["I_PROC_TYPE"] = "S";		//D:조회, S:저장
	
			
	var tables:Object = new Object();
	tables.T_ITAB = arrayList;
	
	
	this.commProxy.exSaveData("Z_HR_HOUSE_SURVEY", param, tables, saveResult, saveFault);
}

public function saveResult(event:ResultEvent):void
{
	if(event.result["RETCODE"]==0) {
		this.closePop(null);
		yearAdjustmentMediator.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
	}
	else {
		msgStr = event.result["RETTEXT"];
		PsHrAlert.showMessage(msgStr);
	}
}
		
private function saveFault(event:FaultEvent):void
{
	msgStr = event.fault.faultString;
	PsHrAlert.showMessage(msgStr);
}

private function setMainTitle(data:ArrayCollection):void{
	//this.lb0.text = "▶세대주 여부 : "+data[0]["HSHLD"];
	this.lb1.text = data[0]["SUR01_T"];
	this.lb2.text = data[0]["SUR02_T"];
	this.lb3.text = data[0]["SUR031_T"];
	this.lb4.text = data[0]["SUR032_T"];
	this.lb5.text = data[0]["SUR033_T"];
	this.lb6.text = data[0]["SUR034_T"];
	this.lb7.text = data[0]["SUR04_T"];
	this.lb8.text = data[0]["SUR05_T"];
}

private function setSubTitle():void{
	this.comboInit();
	
	if(data1[0]["SUR01"] == ""){
			
	}else if(data1[0]["SUR01"] == "1"){
		this.com0.enabled =  true;// 1)
		this.com1.enabled =  true;// 2)
		this.com2.enabled =  false;// 3.1)
		this.datef1.enabled = false;// 3.2)
		this.com3.enabled =  false;// 3.3)
		this.datef2.enabled = false;// 3.4)
		this.com4.enabled =  true;// 4)
	}else{
		this.com0.enabled =  true;// 1)
		this.com1.enabled =  false;// 2)
		this.com2.enabled =  true;// 3.1)
		this.datef1.enabled = true;// 3.2)
		this.com3.enabled =  true;// 3.3)
		this.datef2.enabled = true;// 3.4)
		this.com4.enabled =  true;// 4)
	}
	
	if(data1[0]["SUR01"]!=""){
		//세대주여부 
		if(data1[0]["HSHLD"] == 'N') {
			this.cbHSHLD.selectedIndex = 0;
		}
		else {
			this.cbHSHLD.selectedIndex = 1;
		}
		// 체크박스
		if(data1[0]["SUR05"] == 'Y') {
			this.ckHSHLD.selected = true;
		}
		else {
			this.ckHSHLD.selected = false;
		}		
		
		var index:Number;
		
		index = Number(data1[0]["SUR01"]);
		this.com0.selectedIndex = index;
		
		index = Number(data1[0]["SUR02"]);
		this.com1.selectedIndex = index;
		
		index = Number(data1[0]["SUR031"]);
		this.com2.selectedIndex = index;
		
		if(data1[0]["SUR032"]!=null){
			this.datef1.text= DateUtil.toDefaultString(DateUtil.stringToDateTime(data1[0]["SUR032"]));
		}
		
		index = Number(data1[0]["SUR033"]);
		this.com3.selectedIndex = index;
		
		if(data1[0]["SUR034"]!=null){
			this.datef2.text= DateUtil.toDefaultString(DateUtil.stringToDateTime(data1[0]["SUR034"]));
		}
		
		index = Number(data1[0]["SUR04"]);
		this.com4.selectedIndex = index;
	}
}

private function setComboBox(data:ArrayCollection):void
{
	popHouseNotice["data2_"+headerCount] = data;
	this["com"+headerCount].dataProvider = data;
	this["com"+headerCount].dataProvider.addItemAt({CODE:"", TEXT:"선택 해 주세요."},0);
	this["com"+headerCount].selectedIndex = 0;
}

private function comboInit():void{
	for(var i:uint=1;i<5;i++){
		this["com"+i].enabled=false;
		this["com"+i].selectedIndex=0;
	}
	this.datef1.enabled=false;
	this.datef2.enabled=false;
	this.datef1.text = "";
	this.datef2.text = "";
}

private function changeCombo(event:Event):void{
	var combo:ComboBox = ComboBox(event.target);
	
	if(combo.id=="com0"){
		this.comboInit();
		
		if(combo.selectedItem["CODE"]==""){
			
		}else if(combo.selectedItem["CODE"]=="1"){
			this.com0.enabled =  true;// 1)
			this.com1.enabled =  true;// 2)
			this.com2.enabled =  false;// 3.1)
			this.datef1.enabled = false;// 3.2)
			this.com3.enabled =  false;// 3.3)
			this.datef2.enabled = false;// 3.4)
			this.com4.enabled =  true;// 4)
			
		}else{
			this.com0.enabled =  true;// 1)
			this.com1.enabled =  false;// 2)
			this.com2.enabled =  true;// 3.1)
			this.datef1.enabled = true;// 3.2)
			this.com3.enabled =  true;// 3.3)
			this.datef2.enabled = true;// 3.4)
			this.com4.enabled =  true;// 4)
		}
	}
	
}

private function updateData():void
{
	var arrayList:Array=new Array();
	
	var data:Object={};
	data["SUR01"] = com0.enabled ? com0.selectedItem["CODE"] : ""; 	
	data["SUR02"] = com1.enabled ? com1.selectedItem["CODE"] : ""; 
	data["SUR031"] = com2.enabled ? com2.selectedItem["CODE"] : ""; 	
	data["SUR032"] = datef1.text;	
	data["SUR033"] = com3.enabled ? com3.selectedItem["CODE"] : ""; 	
	data["SUR034"] = datef2.text;	
	data["SUR04"] = com4.enabled ? com4.selectedItem["CODE"] : "";
	
	//2014
	data["SUR05"] = ckHSHLD.selected ? "Y" : "N";
	data["HSHLD"] = cbHSHLD.selectedItem["CODE"];
	
	
	data["SUR01_T"] = data1[0]['SUR01_T'];
	data["SUR02_T"] = data1[0]['SUR02_T'];
	data["SUR031_T"] = data1[0]['SUR031_T'];
	data["SUR032_T"] = data1[0]['SUR032_T'];
	data["SUR033_T"] = data1[0]['SUR033_T'];
	data["SUR034_T"] = data1[0]['SUR034_T'];
	data["SUR04_T"] = data1[0]['SUR04_T'];
	
	//data["HSHLD"] = data1[0]['HSHLD']; 	
	
	
	arrayList.push(data);
	this.save(this.yearPernr, this.sealYear, arrayList);
}

private function dateChange(event:Event):void{
	
}

